<template>
  <div class="admin-login-container">
    <div class="login-box">
      <h2>在线教育平台 - 管理员登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入管理员用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loading" style="width: 100%">登录</el-button>
        </el-form-item>
        <div class="login-footer">
          <router-link to="/login">返回用户登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const loginFormRef = ref(null)
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 模拟管理员登录API调用
    setTimeout(() => {
      // 模拟管理员数据
      const mockAdminInfo = {
        id: 1,
        username: loginForm.username,
        name: '管理员',
        isAdmin: true,
        isSuperAdmin: loginForm.username === 'admin' // 假设admin是超级管理员
      }
      
      const mockToken = 'admin_token_' + Date.now()
      
      // 保存登录状态
      localStorage.setItem('adminToken', mockToken)
      localStorage.setItem('adminInfo', JSON.stringify(mockAdminInfo))
      
      ElMessage.success('登录成功')
      
      // 跳转到管理员首页
      router.push('/admin/dashboard')
    }, 1000)
  } catch (error) {
    console.error('登录验证失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.admin-login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-box {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
  font-size: 24px;
}

.login-wrapper {
  width: 100%;
  max-width: 400px;
}

.login-title {
  color: #303133;
  margin-bottom: 8px;
  font-size: 28px;
}

.login-subtitle {
  color: #606266;
  margin-bottom: 30px;
  font-size: 16px;
}

.login-form .el-form-item {
  margin-bottom: 24px;
}

.login-form .el-input__wrapper {
  border-radius: 8px;
}

.login-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
}

.login-footer {
  margin-top: 20px;
  text-align: center;
}

.login-footer a {
  color: #409eff;
  text-decoration: none;
}

.login-footer a:hover {
  color: #66b1ff;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-login-container {
    padding: 10px;
  }
  
  .login-box {
    padding: 20px;
    width: 95%;
    max-width: 400px;
  }
  
  .login-box h2 {
    font-size: 20px;
    margin-bottom: 20px;
  }
}
</style>